﻿using System.Collections.Generic;
using AutoMapper;
using FluentValidation.Results;
using Turquoise.Entities.Complex.BaseModels;
using Turquoise.Entities.Data;
using Turquoise.Validation.Entities;

namespace Turquoise.Validation.FluentValidation.ContentTypeValidation
{
    public class AddValidator : BaseValidator
    {
        public override bool IsValid { get; set; }
        private AddRules _rules;
        private readonly AddModel<ContentType> _model;

        public AddValidator(AddModel<ContentType> model)
        {
            _model = model;
        }

        public override List<TurquoiseValidationResult> Validate()
        {
            _rules = new AddRules();
            var results = _rules.Validate(_model);
            var mapperConfiguration = new MapperConfiguration(cfg => cfg.CreateMap<ValidationFailure, TurquoiseValidationResult>());
            var mapper = mapperConfiguration.CreateMapper();
            foreach (var error in results.Errors)
            {
                ValidationResults.Add(mapper.Map<ValidationFailure, TurquoiseValidationResult>(error));
            }
            IsValid = results.IsValid;
            return ValidationResults;
        }
    }
}
